#include "queue.h"

//new node malloc
QNode* NewNode1(QDataType val)
{
	QNode* newqnode = (QNode*)malloc(sizeof(QNode));
	assert(newqnode);
	newqnode->data = val;
	newqnode->next = NULL;
	return newqnode;
}

//queue init
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

//queue destroy
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* temppq = pq->phead;
	while (temppq)
	{
		QNode* next = temppq->next;
		free(temppq);
		temppq = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

//queue tail push
void QueuePush(Queue* pq, QDataType value)
{
	assert(pq);
	QNode* newqnode = NewNode1(value);
	if (pq->phead == NULL)
	{
		pq->phead = newqnode;
		pq->ptail = newqnode;
	}
	else
	{
		pq->ptail->next = newqnode;
		pq->ptail = newqnode;
	}
	pq->size ++;
}

//queue head pop
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->phead && pq->ptail);
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QNode* temppq = pq->phead;
		pq->phead = pq->phead->next;
		free(temppq);
		temppq = NULL;
	}
	pq->size --;
}

//queue front value
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->data;
}

//queue back value
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->data;
}

//queue size
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

//queue empty
//if queue==empty return true
//else return false
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return(pq->size == 0);
}


